package com.junye.googleplay.ui.activities;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.junye.googleplay.R;
import com.junye.googleplay.UIUtils.UIUtils;
import com.junye.googleplay.ui.fragment.BaseFragment;
import com.junye.googleplay.ui.fragment.FragmentFactory;
import com.junye.googleplay.ui.view.PagerTab;

public class MainActivity extends BaseActivity {

    private PagerTab mPagerTab;
    private ViewPager mViewPager;
    private MyAdapter myAdapter;
    private  String[] tabNames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPagerTab = (PagerTab) findViewById(R.id.pagertab);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        myAdapter = new MyAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(myAdapter);
        mPagerTab.setViewPager(mViewPager);
    }

    class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
            tabNames = UIUtils.getStringArray(R.array.tab_names);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabNames[position];
        }

        @Override
        public Fragment getItem(int position) {
            BaseFragment fragment = FragmentFactory.CreateFragment(position);
            Log.e("wj", "fragment == "+fragment);
            return fragment;
        }

        @Override
        public int getCount() {
            return tabNames.length;
        }
    }
}
